package com.sea.base.ext.global

import android.app.Activity
import android.app.Application
import android.os.Build
import android.os.Bundle
import com.sea.base.R
import com.sea.base.utils.AppUtil

inline val singleApplication get() = AppUtil.instance.context

inline val isDebug get() = AppUtil.instance.isDebug

inline val buildType get() = AppUtil.instance.buildType

inline val buildTime get() = AppUtil.instance.buildTime

var appVersionName: String = ""
    private set
    get() {
        if (field.isEmpty()) {
            field = try {
                singleApplication.packageManager.getPackageInfo(appPackageName, 0).versionName
            } catch (e: Exception) {
                e.printStackTrace()
                null
            } ?: "unknown"
        }
        return field
    }

var appVersionCode: Long = 0L
    private set
    get() {
        if (field == 0L) {
            val pi = singleApplication.packageManager.getPackageInfo(appPackageName, 0)
            field = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) pi.longVersionCode else pi.versionCode.toLong()
        }
        return field
    }

val appPackageName: String
    get() = singleApplication.packageName

val appName: String
    get() = singleApplication.resources.getString(R.string.app_name)

/**
 * 是否是开发人员的debug包
 */
inline val isDeveloper get() = buildType == "debug"

/**
 * 注册activity回调inline版
 */
inline fun Application.registerActivityLifecycleCallbacks(
    crossinline onCreated: Application.ActivityLifecycleCallbacks.(activity: Activity, savedInstanceState: Bundle?) -> Unit = { _, _ -> },
    crossinline onStarted: Application.ActivityLifecycleCallbacks.(activity: Activity) -> Unit = {},
    crossinline onResumed: Application.ActivityLifecycleCallbacks.(activity: Activity) -> Unit = {},
    crossinline onPaused: Application.ActivityLifecycleCallbacks.(activity: Activity) -> Unit = {},
    crossinline onStopped: Application.ActivityLifecycleCallbacks.(activity: Activity) -> Unit = {},
    crossinline onSaveInstanceState: Application.ActivityLifecycleCallbacks.(activity: Activity, outState: Bundle) -> Unit = { _, _ -> },
    crossinline onDestroyed: Application.ActivityLifecycleCallbacks.(activity: Activity) -> Unit = {},
): Application.ActivityLifecycleCallbacks {
    val callback = object : Application.ActivityLifecycleCallbacks {
        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            onCreated.invoke(this, activity, savedInstanceState)
        }

        override fun onActivityStarted(activity: Activity) {
            onStarted.invoke(this, activity)
        }

        override fun onActivityResumed(activity: Activity) {
            onResumed.invoke(this, activity)
        }

        override fun onActivityPaused(activity: Activity) {
            onPaused.invoke(this, activity)
        }

        override fun onActivityStopped(activity: Activity) {
            onStopped.invoke(this, activity)
        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
            onSaveInstanceState.invoke(this, activity, outState)
        }

        override fun onActivityDestroyed(activity: Activity) {
            onDestroyed.invoke(this, activity)
        }

    }
    registerActivityLifecycleCallbacks(callback)
    return callback
}

/**
 * 当下一个activity create后回调，并删除回调
 */
inline fun Application.doOnNextActivityCreated(
    crossinline onNextActivityCreated: (activity: Activity) -> Unit
): Application.ActivityLifecycleCallbacks {
    return registerActivityLifecycleCallbacks(onCreated = { activity, _ ->
        onNextActivityCreated(activity)
        unregisterActivityLifecycleCallbacks(this)
    })
}